package l1j.server.server.model.damage.hit;

import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.damage.L1Dmg;
import l1j.server.server.templates.L1MagicDoll;
import l1j.server.server.utils.Random;

// 用於Npc_Pc,Pc_Pc 場合
public class L1OnPcHit {

	private L1PcInstance _target;

	public L1OnPcHit(L1PcInstance target) {
		_target = target;
	}

	public int hitRateOnPc(int _hitRate) {
		int attackerDice = Random.nextInt(20) + 1 + _hitRate - 10;

		// 閃避率
		attackerDice -= _target.getDodge();
		attackerDice += _target.getNdodge();

		int defenderDice = 0;

		int defenderValue = (int) (_target.getAc() * 1.5) * -1;

		if (_target.getAc() >= 0) {
			defenderDice = 10 - _target.getAc();
		} else if (_target.getAc() < 0) {
			defenderDice = 10 + Random.nextInt(defenderValue) + 1;
		}

		int fumble = _hitRate - 9;
		int critical = _hitRate + 10;

		if (attackerDice <= fumble) {
			_hitRate = 0;
		} else if (attackerDice >= critical) {
			_hitRate = 100;
		} else {
			if (attackerDice > defenderDice) {
				_hitRate = 100;
			} else if (attackerDice <= defenderDice) {
				_hitRate = 0;
			}
		}

		if (L1MagicDoll.getDamageEvasionByDoll(_target) > 0) {
			_hitRate = 0;
		}
		return _hitRate;
	}

	// ●●●● カウンターバリアのダメージを算出 ●●●●
	public void actionCounterBarrier(L1Dmg dmg) {
		L1ItemInstance weapon = null;
		weapon = _target.getWeapon();
		if (weapon != null) {
			if (weapon.getItem().getType() == 3) { // 両手剣
				// (BIG最大ダメージ+強化数+追加ダメージ)*2
				dmg.add(weapon.getItem().getDmgLarge())
						.add(weapon.getEnchantLevel())
						.add(weapon.getItem().getDmgModifier()).mul(2).hurtTarget(true);
			}
		}
	}

}
